package com.test1;

import java.util.ArrayList;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
import android.view.SurfaceHolder;

public class GameScene implements GameListener{
	private ArrayList< GameLayer > layers = new ArrayList< GameLayer >();
	private ArrayList< GameObject > fruit =  new ArrayList< GameObject >();
	private GameView view = null;
	private SurfaceHolder holder = null;
	private int speed = 5;
	private long gap = 1000;
	
	public GameScene(){
		// create layers;
		for( int i = 0; i < 3; i++ ){
			layers.add( i, new GameLayer() );
		}
		
		//create view;
		view = new GameView( this );
		holder = view.getHolder();
		//create Objects;
		Balana b = Pool.getBalana();
		fruit.add( b );
		this.add( b , GameLayer.game, 10 );
		
		CountDown result = new CountDown();
		result.x = view.getWidth()/2;
		this.add( result, GameLayer.game , 99 );
	}
	
	public void draw(){
		Canvas canvas = holder.lockCanvas();
		Log.d( "scene.draw", canvas.getWidth()+"|"+canvas.getHeight() );
		synchronized(holder){
			canvas.drawColor( Color.BLUE );
//			canvas.drawBitmap( t, 10, 10, null);
			for( int i = 0; i < 3; i++ ){
				GameLayer layer = layers.get( i );
				layer.draw( canvas );
			}
		}
		holder.unlockCanvasAndPost( canvas );
	}
	
	public void update( long e ){
		for( int i = 0; i < layers.size(); i++ ){
			GameLayer layer = layers.get( i );
			layer.update( e );
		}
		
		//水果下落
		gap -= e;
		if( gap <= 0 ){
			for( int i = 0; i < fruit.size(); i++ ){
				GameObject child = fruit.get( i );
				child.y += speed;
				if( child.y > view.getHeight() ){
					fruit.remove( i );
					i --;
					child.remove();
				}
			}
			gap += 1000;
			
			//随机出现新水果
			if( Math.random() < 0.1 ){
				Balana b = Pool.getBalana();
				fruit.add( b );
				this.add( b , GameLayer.game , 10 );
				b.x = ( int )( Math.random() * view.getWidth() );
				if( b.x + b.w > view.getWidth() ){
					b.setPosition( view.getWidth() - b.w, b.y );
				}
			}
		}
	}
	
	public void click( int x, int y ){
		
	}
	
	public boolean add( GameObject obj, int layer ){
		layers.get( layer ).add( obj );
		return true;
	}
	
	public boolean add( GameObject obj, int layer, int depth ){
		layers.get( layer ).add( obj, depth );
		return true;
	}
	
	public GameView getView(){
		return view;
	}

	public void onPress( GameEvent event ) {
		// TODO Auto-generated method stub
		for( int i = 0; i < 3; i++ ){
			layers.get( i ).onPress( event );
		}
	}

	public void onRelease( GameEvent event ) {
		// TODO Auto-generated method stub
		
	}

	public void onClick( GameEvent event  ) {
		// TODO Auto-generated method stub
		
	}

	public void onDragBegin( GameEvent event  ) {
		// TODO Auto-generated method stub
		
	}

	public void onDrag( GameEvent event  ) {
		// TODO Auto-generated method stub
		
	}

	public void onDragEnd( GameEvent event  ) {
		// TODO Auto-generated method stub
		
	}
}
